<?php
namespace Home\Controller;


class IndexController extends AdminController {


    private $_model = null; //数据库操作类
    //初始化操作
	public function _initialize(){	
		$this->_model = D('User');
	}
    //执行添加操作
	public function doadd(){
		if(!$this->_model->create()){
			$this->error($this->_model->getError());
			exit;
		}
		if($this->_model->add() > 0){
			$this->success("添加成功！",U('Index/index'));
		}else{
			$this->error("添加失败！");
		}
	}
	//执行登陆
	public function dologin()
	{
		//接收用户名和密码
		$username = I('post.username');
		$password = I('post.password');

		//验证
		$user = M('User');
		$data = $user->where(array('username'=>$username))->find();
		if (!$data) {
			$this->error('用户名不存在！');
			exit;
		}
		//验证密码
		if ($data['password'] != md5($password)) {
			$this->error('密码不正确');
			exit;
		}
		//把用户信息添加到session
		$_SESSION['admin_user'] = $data;
		
		//跳转到首页
		$this->redirect('Public/index');
	}
	//退出登陆
	public function logout()
	{
		//清空session
		unset($_SESSION['admin_user']);
		$this->display('Index/index');
	}
	
    public function index(){
    	$this->assign("title","乐阅网");
       	$this->display();
    }

}








